'use strict';

const Controller = require('egg').Controller;

// 挖矿合约
class MiningController extends Controller {
  // 添加一个token池子
  async addToken() {
    const { app, ctx, service } = this;
    // token地址
    const tokenAddress = ctx.request.body.tokenAddress;
    // 名字
    const name = ctx.request.body.name;
    // 判断地址的合法性
    if(service.eth.isAddress(tokenAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 判断名字的合法性, 只能是字母
    if(/^[a-zA-Z]+$/.test(name) === false) {
      ctx.body = { code: 1001, msg: '名字只能是字母, 不合法;', data: {} };
      return;
    }

    try {
      const hash = await service.mining.addToken2(tokenAddress, name);
      await service.eth.myGetTransaction2(hash);
      ctx.body = { code: 1000, msg: '发送成功'};
    }catch (e) {
      ctx.body = { code: 1002, msg: '合约交易错误', err: e.toString() };
    }
  }

  // 添加一个lp池子
  async addLp() {
    const { app, ctx, service } = this;
    // token地址
    const lpAddress = ctx.request.body.lpAddress;
    // 名字
    const name = ctx.request.body.name;
    // 判断地址的合法性
    if(service.eth.isAddress(lpAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 判断名字的合法性, 只能是字母
    if(/^[a-zA-Z]+$/.test(name) === false) {
      ctx.body = { code: 1001, msg: '名字只能是字母, 不合法;', data: {} };
      return;
    }

    try {
      const hash = await service.mining.addLp2(lpAddress, name);
      await service.eth.myGetTransaction2(hash);
      ctx.body = { code: 1000, msg: '发送成功'};
    }catch (e) {
      ctx.body = { code: 1002, msg: '合约交易错误', err: e.toString() };
    }
  }

  // 获取所有的token池子
  async getPools() {
    const { app, ctx, service } = this;
    const res = await service.mining.getPools2();
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 开启获取关闭某个池子
  async openOrClosePool() {
    const { app, ctx, service } = this;
    // pool地址
    const poolAddress = ctx.request.body.poolAddress;
    // 判断地址的合法性
    if(service.eth.isAddress(poolAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }

    try {
      const hash = await service.mining.openOrClosePool2(poolAddress);
      await service.eth.myGetTransaction2(hash);
      ctx.body = { code: 1000, msg: '发送成功'};
    }catch (e) {
      ctx.body = { code: 1002, msg: '合约交易错误', err: e.toString() };
    }
  }

  // 查询某个池子的详情
  async getPoolMsg() {
    const { app, ctx, service } = this;
    const poolAddress = ctx.request.body.poolAddress;
    console.log(poolAddress)
    const res = await service.mining.poolMapping2(poolAddress);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 查询用户的某个池子质押信息
  async getUserPoolMsg() {
    const { app, ctx, service } = this;
    const poolAddress = ctx.request.body.poolAddress;
    const userAddress = ctx.request.body.userAddress;
    if(service.eth.isAddress(poolAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }

    const res = await service.mining.getUserPoolMsg2(poolAddress, userAddress);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 查询用户地址是否是有效用户
  async isUser() {
    const { app, ctx, service } = this;
    const userAddress = ctx.request.body.userAddress;
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }

    const res = await service.mining.isUser2(userAddress);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 查询双重签名是否使用
  async signHashStatus() {
    const { app, ctx, service } = this;
    const messageHash = ctx.request.body.messageHash;
    const res = await service.mining.signHashStatus2(messageHash);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 用户提取质押收益的二次签名
  async secondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);


    // 验证的数据有: 池子地址, 用户地址, 上级地址, 上上级地址, 用户数量, 上级数量, 上上级数量, 随机数;
    const poolAddress = ctx.request.body.poolAddress;
    const userAddress = ctx.request.body.userAddress;
    const superAddress = ctx.request.body.superAddress;
    const superSuperAddress = ctx.request.body.superSuperAddress;
    const value = ctx.request.body.value;
    const superValue = ctx.request.body.superValue;
    const superSuperValue = ctx.request.body.superSuperValue;
    const nonce = ctx.helper.createNonce();
    // const nonce = '1633938591900';


    // 验证地址
    if(service.eth.isAddress(poolAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(superAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(superSuperAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 金额必须大于0, 不能有小数
    if(ctx.helper.lte(value, '0') || ctx.helper.isDot(value)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(superValue, '0') || ctx.helper.isDot(superValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(superSuperValue, '0') || ctx.helper.isDot(superSuperValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }

    const poolAddress0x = await service.eth.addressToHex2(poolAddress);
    const userAddress0x = await service.eth.addressToHex2(userAddress);
    const superAddress0x = await service.eth.addressToHex2(superAddress);
    const superSuperAddress0x = await service.eth.addressToHex2(superSuperAddress);
    // 开始签名
    const res = await service.mining.secondSignature2(
      poolAddress0x, userAddress0x, superAddress0x, superSuperAddress0x, value, superValue, superSuperValue, nonce
    );
    const data = {
      // 默认地址
      poolAddress: poolAddress,
      userAddress: userAddress,
      superAddress: superAddress,
      superSuperAddress: superSuperAddress,
      // 转换之后的地址
      poolAddress0x: poolAddress0x,
      userAddress0x: userAddress0x,
      superAddress0x: superAddress0x,
      superSuperAddress0x: superSuperAddress0x,
      // 其它值
      value: value,
      superValue: superValue,
      superSuperValue: superSuperValue,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 扫描质押事件
  async scanDeposit() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Deposit';
    const res = await service.mining.scanEvent(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,
        poolAddress0x: res[i].result.pool,
        userAddress0x: res[i].result.owner,
        value: res[i].result.value,
        poolAddress: await service.eth.addressFromHex(res[i].result.pool),
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 扫描提现事件
  async scanWithdraw() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Withdraw';
    const res = await service.mining.scanEvent(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,
        poolAddress0x: res[i].result.pool,
        userAddress0x: res[i].result.owner,
        value: res[i].result.value,
        poolAddress: await service.eth.addressFromHex(res[i].result.pool),
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 扫描用户提取收益事件
  async scanUserRed() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'UserRed';
    const res = await service.mining.scanEvent(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,
        poolAddress0x: res[i].result.pool,
        userAddress0x: res[i].result.owner,
        superAddress0x: res[i].result.superAddress,
        superSuperAddress0x: res[i].result.superSuperAddress,
        value: res[i].result.value,
        superValue: res[i].result.superValue,
        superSuperValue: res[i].result.superSuperValue,
        poolAddress: await service.eth.addressFromHex(res[i].result.pool),
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
        superAddress: await service.eth.addressFromHex(res[i].result.superAddress),
        superSuperAddress: await service.eth.addressFromHex(res[i].result.superSuperAddress),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }




}

module.exports = MiningController;
